package com.onetech.adrewards.free.models;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onetech.adrewards.free.OneTech_ApplicationContext;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.config.AppConfig.URLRequestApi;
import com.onetech.adrewards.free.http.APIRequestResult;
import com.onetech.adrewards.free.http.ZuZuHttpRequest;
import com.onetech.adrewards.free.listener.PointHistoryIF;
import com.onetech.adrewards.free.objects.PointHistoryDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.zuzu.db.store.SimpleStoreIF;

public class PointHistoryModel implements PointHistoryIF {
	private static PointHistoryIF _instance;
	private static final Lock createLock = new ReentrantLock();
	private static final int STORE_EXPIRE = 2 * 60;

	public PointHistoryModel() {
		getStoreAdapter();
	}

	public static PointHistoryIF getInstance() {
		if (_instance == null) {
			createLock.lock();
			if (_instance == null) {
				_instance = new PointHistoryModel();
			}
			createLock.unlock();
		}
		return _instance;
	}

	private SimpleStoreIF getStoreAdapter() {
		return AdrewadsUtils.getStoreAdapter(Cache.POINT_HISTORY_NAME,
				OneTech_ApplicationContext.getContext(), Cache.POINT_HISTORY_NUMBER);
	}

	public void setStore(String key, Object item) {
		if (item == null) {
			return;
		}
		Gson gson = new Gson();
		String temp = gson.toJson(item);
		this.getStoreAdapter().put(key, temp, STORE_EXPIRE);
	}

	

	private List<PointHistoryDTO> deserializeInteger(String json) {
		List<PointHistoryDTO> result = null;
		if (json == null || json.equals(""))
			return result;
		try {
			result = new ArrayList<PointHistoryDTO>();
			Gson gson = new Gson();
			Type listType = new TypeToken<List<PointHistoryDTO>>() {
			}.getType();
			result = (List<PointHistoryDTO>) gson.fromJson(json, listType);
		} catch (Exception e) {
			return null;
		}
		return result;
	}

	@Override
	public List<PointHistoryDTO> getPointHistory(String pageNo) {
		String json=getStoreAdapter().get(Cache.POINT_HISTORY_KEY+pageNo);
		List<PointHistoryDTO> result = deserializeInteger(json);
		if(result!=null){
			return result;
		}
		Bundle params=new Bundle();
		AdrewadsUtils.checkUId();
		APIRequestResult data=ZuZuHttpRequest.getInstance().getDataFromServer(URLRequestApi.GET_POINT_HISTORY+AppConfig.uId+"/"+pageNo, params);		
		result = parserPointHistoryFromAPIRequestResult(data);
		if(result!=null){
			setStore(Cache.POINT_HISTORY_KEY+pageNo, result);
		}
		return result;
	}	
	private List<PointHistoryDTO> parserPointHistoryFromAPIRequestResult(APIRequestResult data){
		try {
			List<PointHistoryDTO> result=null;			
			if (data.error_code.equals("0") && data.data != null && !data.data.equals("")) {				
				JSONObject jsonObject=(JSONObject)data.data;				
				JSONArray jArr=jsonObject.getJSONArray("result");
				result=new ArrayList<PointHistoryDTO>();
				for (int i = 0; i < jArr.length(); i++) {
					PointHistoryDTO temp=parserPointHistoryData(jArr.getJSONObject(i));
					if(temp!=null){
						result.add(temp);
					}
				}												
			}
			return result;
		} catch (Exception e) {
			return null;
		}
	}
	private PointHistoryDTO parserPointHistoryData(JSONObject jb){
		try {
			PointHistoryDTO result=new PointHistoryDTO();
			result.setAd_name(jb.getString("ad_name"));
			result.setCoin(jb.getString("coin"));
			result.setCreated_at(jb.getString("created_at"));
			return result;
		} catch (Exception e) {
			return null;
		}
	}
}
